package com.bluetrum.devicemanager.cmd.payloadhandler;

import androidx.annotation.NonNull;

import com.bluetrum.devicemanager.models.LeAudioBisItem;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class LeAudioBisListAddPayloadHandler extends PayloadHandler<List<LeAudioBisItem>> {

    public LeAudioBisListAddPayloadHandler(@NonNull byte[] payload) {
        super(payload);
    }

    @Override
    public List<LeAudioBisItem> call() {
        final byte[] payload = getPayload();

        List<LeAudioBisItem> list = new ArrayList<>();

        ByteBuffer bb = ByteBuffer.wrap(payload)
                .order(ByteOrder.LITTLE_ENDIAN);

        int size = bb.get();
        for (int i = 0; i < size; i++) {
            Integer identifier = null;
            byte[] broadcastAddressBytes = null;
            Integer rssi = null;
            String broadcastName = null;
            String programInfo = null;
            Boolean connected = null;

            while (bb.remaining() >=2) {
                byte type = bb.get();
                int length = bb.get();

                switch (type) {
                    case LeAudioBisItem.TYPE_IDENTIFIER: {
                        if (length == 4) {
                            identifier = bb.getInt();
                        } else {
                            // 格式错误，直接拜拜
                            return null;
                        }
                    } break;
                    case LeAudioBisItem.TYPE_BROADCAST_ADDRESS: {
                        if (length == 6) {
                            broadcastAddressBytes = new byte[6];
                            bb.get(broadcastAddressBytes);
                        } else {
                            // 格式错误，直接拜拜
                            return null;
                        }
                    } break;
                    case LeAudioBisItem.TYPE_RSSI: {
                        if (length == 1) {
                            rssi = (int) bb.get();
                        } else {
                            // 格式错误，直接拜拜
                            return null;
                        }
                    } break;
                    case LeAudioBisItem.TYPE_BROADCAST_NAME: {
                        byte[] broadcastNameBytes = new byte[length];
                        bb.get(broadcastNameBytes);
                        broadcastName = new String(broadcastNameBytes, StandardCharsets.UTF_8);
                    } break;
                    case LeAudioBisItem.TYPE_PROGRAM_INFO: {
                        byte[] programInfoBytes = new byte[length];
                        bb.get(programInfoBytes);
                        programInfo = new String(programInfoBytes, StandardCharsets.UTF_8);
                    } break;
                    case LeAudioBisItem.TYPE_CONNECTION_STATUS: {
                        if (length == 1) {
                            byte connectionByte = bb.get();
                            if (connectionByte == 0x01) {
                                connected = true;
                            } else if (connectionByte == 0x00) {
                                connected = false;
                            } else {
                                // 格式错误，直接拜拜
                                return null;
                            }
                        } else {
                            // 格式错误，直接拜拜
                            return null;
                        }
                    } break;
                }
            }

            if (identifier != null &&
                    broadcastAddressBytes != null && broadcastAddressBytes.length == 6 &&
                    rssi != null &&
                    broadcastName != null &&
                    programInfo != null &&
                    connected != null) {
                LeAudioBisItem item =
                        new LeAudioBisItem(
                                identifier,
                                broadcastAddressBytes,
                                rssi,
                                broadcastName,
                                programInfo,
                                connected
                        );
                list.add(item);
            } else {
                // 格式不对，直接返回null
                return null;
            }
        }

        return list;
    }
}
